package top.xunfong.file.listener;

import top.xunfong.common.core.constant.redisKey.RedisKeyConstants;
import top.xunfong.file.service.FileService;
import top.xunfong.redis.listener.ExpirationListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.stereotype.Component;

@Component
public class BreakpointFileExpirationListener implements ExpirationListener {

    @Autowired
    FileService fileService;

    @Override
    public String getRedisKey() {
        return RedisKeyConstants.BREAKPOINT_METADATA_KEY;
    }

    @Override
    public void onMessage(Message message, byte[] pattern) {
        // 键过期，删除文件
        // 获取键中的md5值
        String md5 = message.toString().split(RedisKeyConstants.BREAKPOINT_METADATA_KEY)[1];
        fileService.deleteFileByMd5(md5);
    }
}
